home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / allocwg.com / README.DOC < prev    next >
Encoding:
Text File  |  1988-11-11  |  8.4 KB  |  175 lines

  1.                      Memory Allocation Procedures
  2.  
  3.  
  4. The procedures included in this archive provide functionally equivalent
  5. memory allocation procedures for the main memory procedures used in C. 
  6. These procedures were written primarily because of a need to minimize
  7. memory usage for a large program under development.  The MSC 5.1
  8. library procedures were not satisfactory for this application because
  9. they did not release memory back to the system when it was 'free'ed. 
  10. Additionally, they tended to allocate too much memory under certain
  11. circumstances which lead to a reduction in space available for other
  12. programs and data which were currently active at the time.
  13.  
  14. These procedures should only be used with large data model programs
  15. (i.e., compact, large, and huge memory models).  No provision has been
  16. made for small or medium model programs.  A brief description of each
  17. file in this archive is given below:
  18.  
  19.    alloc.h      -  Include file which is used to define the memory
  20.                    structures used in processing.  
  21.  
  22.    calloc.c     -  Functional equivalent of calloc.
  23.  
  24.    dispsize.c   -  Procedure which displays the various sizes of the
  25.                    program at the time it is called.  Sizes displayed
  26.                    are code size, default data size used, default data
  27.                    size allocated, memory allocated by user, memory
  28.                    allocated by these procedures to hold user
  29.                    allocation, and total program size.  This is handy
  30.                    in determining when and where memory gets allocated
  31.                    and released.
  32.  
  33.    dumpheap.c   -  Procedure which will traverse through the allocated
  34.                    memory blocks and write to a file the allocation
  35.                    sizes indicating whether they are in use or are
  36.                    free.  This is a handy procedure for determining
  37.                    if information is being freed or not.
  38.  
  39.    free.c       -  Functional equivalent to free.  Included in this
  40.                    file is an additional procedure _ffree which calls
  41.                    free.
  42.  
  43.    malloc.c     -  Functional equivalent of malloc.  This file also 
  44.                    contains the memory structures which are used to
  45.                    keep track of the blocks of memory allocated.  In
  46.                    addition, the procedure _fmalloc is defined which
  47.                    just makes a call to malloc.
  48.  
  49.    memutil.c    -  This file contains a set of procedures which are
  50.                    used by the main memory allocation procedures to
  51.                    maintain the memory structure used to keep track of
  52.                    the user allocations and frees.
  53.  
  54.  
  55.    progsize.c   -  Procedure which when called will return the various
  56.                    sizes indicated above in dispsize.c.  This is the
  57.                    procedure which dispsize.c calls to get it's 
  58.                    information.  
  59.  
  60.    realloc.c    -  Functional equivalent of realloc.
  61.  
  62.    reduceal.c   -  This procedure may be called to reduce the memory
  63.                    allocated to a program.  It attempts to release back
  64.                    to DOS the memory not currently being used.
  65.  
  66.    reducedd.c   -  This procedure may be called to reduce the default
  67.                    data segment to the minimum size required.  Normally
  68.                    this should be called before any calls to memory
  69.                    allocation procedures.  Included in this file are
  70.                    dummy procedures for _nmalloc and _nfree.  These
  71.                    latter procedures were included because some
  72.                    of the MSC library uses the near heap for memory
  73.                    allocation (even though you may be using large
  74.                    data model programs).  Since this procedure does
  75.                    away with any space which might be used for near
  76.                    heap allocation, this forces the procedures which
  77.                    call them to use the 'far' heap.
  78.    
  79.    test.c       -  This is a program which may be used to compare the
  80.                    functionality of these procedures with those found
  81.                    in the MSC 5.1 library.
  82.  
  83.    linker.bat   -  Batch procedure for linking the test program.
  84.  
  85.    load         -  Link directives for the linking the test program.
  86.    
  87. For the most part, all of these procedures are commented and indicate
  88. fairly well what they are doing.  As mentioned above, the main purpose
  89. in writing these procedures was to try to minimize the amount of memory
  90. used.  Consequently, they take extra care in trying to place allocated
  91. memory at the lowest possible memory location so that higher memory, if
  92. freed, can be released back to the operating system.  This is
  93. especially true of 'realloc' which tries very hard to place a
  94. reallocated block at the lowest possible memory location.  Don't assume
  95. that reallocating a block at the same or smaller memory size will
  96. remain at the same starting address in memory (which isn't true with
  97. the realloc in this archive).  This 'realloc' approach can have some
  98. advantages if after allocating and freeing a large number of memory
  99. blocks, you reallocate the blocks you want to save so they percolate
  100. down into low memory.
  101.  
  102. Documentation for these procedures is essentially as indicated in the
  103. MSC 5.1 manuals.  There is a global variable called MBSize which may be
  104. set in the same manner as _amblksiz with the same functionality.  See
  105. the code for malloc.c for further information.  Also be aware that if a
  106. zero length block is requested from malloc, it will return a NULL
  107. pointer as indicated in the MSC 5.1 manuals.  This seems to be
  108. different than what the MSC procedures do because, at least in my
  109. version of MSC 5.1, MSC's malloc returns a non-NULL pointer for a zero
  110. length block request (even though the documentation says otherwise).
  111.  
  112. All of these procedures were tested fairly thoroughly although I won't
  113. guarantee there are no bugs.  The tests indicate some fairly
  114. interesting results when compared to the MSC 5.1 procedures.  Although
  115. I expected them to be slower than the MSC library procedures, they
  116. don't appear to be.  Indeed, in some cases (e.g., realloc(s)), they
  117. seem to be faster.  This was somewhat of a surprise.  Additionally,
  118. they use about the same or less memory in most cases tested.  The test
  119. program indicated above may be used to verify this (actually, you
  120. should do this to assure yourself that these procedures do what I have
  121. indicated).
  122.    
  123. To do a comparison between these procedures and the MSC procedures, do
  124. the following:
  125.  
  126.    1.  Unarchive these procedures into a working directory.
  127.  
  128.    2.  Compile the procedures with the following command line:
  129.  
  130.           cl /c /AL *.c
  131.  
  132.        This will compile the program and procedures for testing the
  133.        memory procedures contained in this archive.
  134.  
  135.    3.  Link the objects by executing the batch file 'linker.bat'.
  136.  
  137.    4.  Clear the screen and execute 'test'.  The test program will
  138.        perform a number of allocations, reallocations, and frees.
  139.        At the end of each activity (e.g., a number of allocations),
  140.        it will perform a heap dump to the file 'memory.new'.
  141.  
  142.    5.  Recompile the procedures with the following command line:
  143.  
  144.           cl /c /AL /DMSC *.c
  145.  
  146.        This will compile the program for testing with the MSC library
  147.        procedures.
  148.  
  149.    6.  Line the objects by executing the batch file 'linker.bat'.
  150.  
  151.    7.  Clear the screen and execute 'test'.  The same test will be
  152.        performed as that indicated in 4 above with the exception that
  153.        the heap dump will be written to a file called 'memory.msc'.
  154.  
  155.    8.  Compare the heap space used by both sets of procedures by 
  156.        comparing the memory dumps.
  157.  
  158. You should try varying the parameters used in the test program to see
  159. what can occur for both sets of these procedures.
  160.  
  161. Something you should be aware of is that these procedures require
  162. approximately 3000 more bytes of program space than the MSC procedures.
  163. However, as may be seen in the test above, if a large number of
  164. allocations and frees are performed, the total execution size may
  165. actually be smaller using these procedures.  Additionally, you have the
  166. capability to release memory back to the system when necessary.
  167.  
  168. If you have any comments, corrections, or improvements, please let me
  169. know.  I am sure there are improvements which can be made.  
  170.  
  171.       
  172.              Willard Gersbacher  - CompuServe User ID: (76117,2611)
  173.  
  174.  
  175.